import {
  observable,
  extendObservable,
  computed,
  toJS,
  action
} from '../../libs/mobx.js';

import {
  reqHeader,
  greedoLayout,
  proxy
} from '../../utils/util.js';
import { communityAlilog } from '../../utils/community_util.js';
import {
  encode
} from '../../libs/querystring/index.js';
import LoginStore from '../login/store.js';
import ContestStore from '../contest/contestStore.js';

var CurStore = function(title) {
  let me = this;
  let getDefault = function(){
    return {
      activeNums: 0,
      activeJson: {},
      activeIndex:null,
      hasNext: true,
      photoList: [],
      url: '',
      page: 1,
      size: 40
    }
  }
  extendObservable(this, getDefault());

  this.reset = action(function(){
    Object.assign(this,getDefault());
  })

  this.setAjaxUrl = action(function(str) {
    this.url = str;
  })


  this.chageActive = action(function(index, options) {
    let curItem = this.photoList[index];

    let activeClick = () => {
      // if (ContestStore.photo && !ContestStore.groupPhoto){
      //   // 只能上传单图
      //   if (this.activeNums) {
      //     this.photoList[this.activeIndex].active = false;
      //     this.activeJson = {};
      //     this.activeNums = 0;
      //   }
      // }
      
      this.activeJson[curItem.id] = true;
      this.activeNums++;
      this.activeIndex = index;
      callback();
    }

    let callback = () => {
      Object.assign(curItem, options);
      this.photoList = Array.apply([], this.photoList);
    }
    if (options.active) {
      if (curItem.openState === "private" && !options.fromEditInfo){
        wx.showModal({
          title: '提示',
          content: '如果私有图片上传到大赛中，其他用户可以通过该大赛浏览到该图片',
          success: (result)=> {
            if (result.confirm) {
              activeClick();
              
            } else if (result.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        activeClick();
      }
    } else {
      delete this.activeJson[curItem.id];
      this.activeNums--;
      callback();
    }
    
  })


  this.loadData = action(() => {
    if (this.url) {
      wx.request({
        url: this.url,
        data: {
          page: this.page,
          size: this.size
        },
        header: reqHeader,
        success: function(res) {
          if (res.statusCode != 200) {
            return;
          }
          res = res.data;
          if (res.status == 200 && res.data.length) {
            if (res.data.length < me.size) {
              me.hasNext = false;
            }
            let tplArray = me.photoList.concat(res.data);
            greedoLayout(tplArray);
            me.photoList = tplArray;
            // me.photoList.push.apply(me.photoList, res.data);
          } else {
            me.hasNext = false;
          }
        }
      })
    } else {
      me.hasNext = false;
    }
  })
  this.postCommunity = action(function () {
    if (this.activeNums) {
      var jsonArray = Object.keys(this.activeJson);
      if (jsonArray.length) {

        wx.request({
          url: `${proxy}/community/tribe/uploadForPersonal`, 
          data: {
            photoIds: jsonArray.join(','),
            // contestCategoryId: app.globalData.selectedCategoryId,
            tribeId: wx.getStorageSync('tribeId'),
            access_token: LoginStore.userInfo.access_token
          },
          header: reqHeader,
          success: function (res) {
            if (res.statusCode != 200) {
              return;
            }
            res = res.data;
            if (res.status == 200) {
              wx.showToast({
                mask: true,
                title: '投稿成功',
                icon: 'success',
                duration: 1000
              })
              setTimeout(function () {
                let pages = getCurrentPages();
                let contestPage = pages[pages.length - 2];
                contestPage.setData({
                  showTheme: false
                }, function () {
                  wx.navigateBack({
                    delta: 1
                  })
                })

              }, 1000)
              //埋点 投稿成功
              communityAlilog('mini-program-tribe-profile-submit-completed');
              //埋点 投稿成功
            } else {

            }
          }
        })
      } else {
        wx.showToast({
          mask: false,
          title: '没有可参赛作品',
          icon: 'none',
          duration: 2000
        })
      }
    }
  });

  this.nextPage = action(function() {
    this.page++;
    this.loadData();
  })
}

export default new CurStore();